package framebl;

import java.rmi.RemoteException;
import java.util.ArrayList;

import userManagebl.Fstream;

import dataFactory.DataFactory;
import frameDataService.FrameDataService;
import message.Message;
import PO.FramePO;
import VO.FrameVO;

public class Frame {
	
	public Message inputFrame(FrameVO frameVO) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		FrameDataService fds=factory.getFrameDataService();
		//
		FramePO framePO=VOToPO(frameVO);
		return fds.add(framePO);
		
	}
	
	public Message modifyFrame(FrameVO oldOne,FrameVO newOne) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		FrameDataService fds=factory.getFrameDataService();
		//
		FramePO theOld=VOToPO(oldOne);
		FramePO theNew=VOToPO(newOne);
		return fds.update(theOld,theNew);
	}
	
	public ArrayList<FrameVO> showFrame() throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		FrameDataService fds=factory.getFrameDataService();
		//
		ArrayList<FramePO> poList=fds.find();
		ArrayList<FrameVO> voList=new ArrayList<FrameVO>();
		for(int i=0;i<poList.size();i++){
			FramePO po=poList.get(i);
			FrameVO vo=POToVO(po);
			voList.add(vo);
		}
		return voList;
	}
	
	public Message addPlan(String location,String dp) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		FrameDataService fds=factory.getFrameDataService();
		//
		Fstream outFile=new Fstream();
		ArrayList<String> plan=outFile.readFile(location);
		return fds.submit(plan, dp);
	}

	
	public ArrayList<String> showPlan(String dp) throws RemoteException {
		//
		DataFactory factory=new DataFactory();
		FrameDataService fds=factory.getFrameDataService();
		//
		return fds.downLoad(dp);
	}

	private FramePO VOToPO(FrameVO vo){
		FramePO po=new FramePO(vo.getModule(),vo.getCourseType(),
				vo.getSN(),vo.getCategory(),vo.getCourseName(),vo.getCredit(),
				vo.getTime());
		return po;
	}
	
	private FrameVO POToVO(FramePO po){
		FrameVO vo=new FrameVO(po.getModule(),po.getCourseType(),
				po.getSN(),po.getCategory(),po.getCourseName(),po.getCredit(),
				po.getTime());
		return vo;
	}
	
	
}
